#include "widget.h"

#include <QApplication>
#include <QFile>
#include <QStandardItemModel>
#include <QTableView>
#include <QTextStream>
#include "dateitemdelegate.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    // Widget w;
    // w.show();

    QTableView tableView;
    QStandardItemModel itemModel(4,4);
    tableView.setModel(&itemModel);

    //日期列
    DateItemDelegate dateDelegate;
    tableView.setItemDelegateForColumn(1, &dateDelegate);

    itemModel.setHeaderData(0, Qt::Horizontal,QObject::tr("姓名"));
    itemModel.setHeaderData(1, Qt::Horizontal,QObject::tr("生日"));
    itemModel.setHeaderData(2, Qt::Horizontal,QObject::tr("职业"));
    itemModel.setHeaderData(3, Qt::Horizontal,QObject::tr("收入"));

    QFile file("d:/test.txt");
    QString line;
    if(file.open(QFile::ReadOnly|QFile::Text)){
        QTextStream textStream(&file);
        textStream.setCodec("UTF-8");
        itemModel.removeRows(0, itemModel.rowCount(QModelIndex()),QModelIndex());//QModelIndex():表示model的最顶级的item项
        int row = 0;
        do {
            line = textStream.readLine();
            if(!line.isEmpty()){
                itemModel.insertRows(row,1, QModelIndex());
                QStringList pieces = line.split(",");
                itemModel.setData(itemModel.index(row,0,QModelIndex()),pieces[0]);
                itemModel.setData(itemModel.index(row,1,QModelIndex()),pieces[1]);
                itemModel.setData(itemModel.index(row,2,QModelIndex()),pieces[2]);
                itemModel.setData(itemModel.index(row,3,QModelIndex()),pieces[3]);
                row++;
            }
        }while(!line.isEmpty());
        file.close();
    }
    tableView.setWindowTitle(QObject::tr("Delegate测试"));
    tableView.show();


    return a.exec();
}
